package apis

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
	"vrcm/src/vrcm/app/admin/models/monitor"
	dto "vrcm/src/vrcm/app/admin/service/dto/monitor"
	service "vrcm/src/vrcm/app/admin/service/monitor"
	"vrcm/src/vrcm/common/apis"
)

type SetManage struct {
	apis.Api
}

// GetPage 获取MonitorBasic列表
// @Summary 获取MonitorBasic列表
// @Description 获取MonitorBasic列表
// @Tags MonitorBasic
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.MonitorBasic}} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic [get]
// @Security Bearer
func (e SetManage) GetPage(c *gin.Context) {
	req := dto.SetManageGetPageReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	list := make([]monitor.SetManage, 0)
	var count int64

	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取SetManage失败，失败信息 %s", err.Error()))
		return
	}
	e.PageOK(list, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取MonitorBasic
// @Summary 获取MonitorBasic
// @Description 获取MonitorBasic
// @Tags MonitorBasic
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.MonitorBasic} "{"code": 200, "data": [...]}"
// @Router /api/v1/monitor-basic/{id} [get]
// @Security Bearer
func (e SetManage) Get(c *gin.Context) {
	req := dto.SetManageGetReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取SetManage失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(data, "查询成功")
}

// Insert 创建MonitorBasic
// @Summary 创建MonitorBasic
// @Description 创建MonitorBasic
// @Tags MonitorBasic
// @Accept application/json
// @Product application/json
// @Param data body dto.MonitorBasicInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/monitor-basic [post]
// @Security Bearer
func (e SetManage) Insert(c *gin.Context) {
	req := dto.SetMangeInsertReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("创建SetManage失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "创建成功")
}

func (e SetManage) Update(c *gin.Context) {
	req := dto.SetMangeUpdateReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(apis.DBCreateError.Code, err, fmt.Sprintf("更新SetManage失败， %s", err.Error()))
		return
	}

	e.OK(req.Id, "更新成功")
}

func (e SetManage) Delete(c *gin.Context) {
	req := dto.SetManageDeleteReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(apis.DBDeleteError.Code, err, fmt.Sprintf("删除SetManage失败， %s", err.Error()))
		return
	}
	e.OK(nil, "删除成功")
}

func (e SetManage) GetEvent(c *gin.Context) {
	req := dto.GetSetReq{}
	s := service.SetManage{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	data, err := s.GetEvent(&req)
	if err != nil {
		e.Error(apis.DBQueryError.Code, err, fmt.Sprintf("获取活动失败， %s", err.Error()))
		return
	}
	e.OK(data, "获取活动成功")
}

func (e SetManage) GetSystemctlTime(c *gin.Context) {
	err := e.MakeContext(c).
		MakeOrm().
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(apis.InternalError.Code, err, err.Error())
		return
	}
	now := time.Now().Unix()
	e.OK(now, "获取活动成功")
}
